//
//  BTAVPreviewView.m
//  BTAVEditor
//
//  Created by TMAC on 2020/2/4.
//  Copyright © 2020 XMac. All rights reserved.
//

#import "BTAVPreviewView.h"

@interface BTAVPreviewView ()
@property (strong, nonatomic) CALayer *overlay;

@property (strong, nonatomic) NSMutableDictionary *faceLayers;

@end

@implementation BTAVPreviewView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupLayer];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupLayer];
    }
    return self;
}

-(void)setupLayer{
    self.faceLayers = @{}.mutableCopy;
    
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1/1000;
    
    self.overlay = [CALayer layer];
    self.overlay.frame = self.bounds;
    self.overlay.sublayerTransform = transform;
    [self.layer addSublayer:self.overlay];
}

+(Class)layerClass{
    return [AVCaptureVideoPreviewLayer class];
}

- (void)setSession:(AVCaptureSession *)session{
    [((AVCaptureVideoPreviewLayer *)self.layer) setSession:session];
}

-(AVCaptureSession *)session{
    return [((AVCaptureVideoPreviewLayer *)self.layer) session];
}

- (CGPoint)captureDevicePointOfInterestForPoint:(CGPoint)pointInLayer{
    return [((AVCaptureVideoPreviewLayer *)self.layer) captureDevicePointOfInterestForPoint:pointInLayer];
}


- (void)didDetectFaces:(NSArray<AVMetadataFaceObject *> *)faces{
    NSMutableArray<AVMetadataFaceObject *> *transformFaces = @[].mutableCopy;
    [faces enumerateObjectsUsingBlock:^(AVMetadataFaceObject * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [transformFaces addObject:(AVMetadataFaceObject *)[((AVCaptureVideoPreviewLayer *)self.layer) transformedMetadataObjectForMetadataObject:obj]];
    }];
    NSMutableArray<NSNumber *> *lostFaces = [self.faceLayers.allKeys mutableCopy];
    [transformFaces enumerateObjectsUsingBlock:^(AVMetadataFaceObject * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSNumber *faceID = @(obj.faceID);
        [lostFaces removeObject:faceID];
        CALayer *layer = self.faceLayers[faceID];
        if (!layer) {
            layer = [self makeFaceLayer];
            [self.overlay addSublayer:layer];
            self.faceLayers[faceID] = layer;
        }
        layer.transform = CATransform3DIdentity;
        CGRect realFrame = CGRectMake(CGRectGetMinX(obj.bounds)-CGRectGetWidth(obj.bounds)*0.15, CGRectGetMinY(obj.bounds)*1.5, CGRectGetWidth(obj.bounds)*1.3, CGRectGetHeight(obj.bounds));
        layer.frame = realFrame;
    }];
    
    [lostFaces enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CALayer *layer = self.faceLayers[obj];
        [layer removeFromSuperlayer];
        [self.faceLayers removeObjectForKey:obj];
    }];
}

-(CALayer *)makeFaceLayer{
    CALayer *layer = [CALayer layer];
    [layer setContents:(__bridge id)[UIImage imageNamed:@"masks"].CGImage];
    return layer;
}


@end
